/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI.XML;

import TDAArbol.*;
import TDALista.*;
import TDAMapeo.*;

/**
 * Controlador del arbol en XML
 * @author Caneva Francisco
 * @author Holtz Jordan
 */

public class XMLDriver<E> {
    public XMLDriver() {
    }
    
    public XMLWriter tree2XML(LinkedTree<E> t) {
        Iterable<Position<E>> list = t.positions();
        XMLWriter xmlw = new XMLWriter(false);
        int lastLevel=Integer.MIN_VALUE, currentLevel=0;
        //Creamos los elementos necesarios para poder hacer la conexion con Prefuse y/o guardar el arbol
        try{ 
            xmlw.openSection("tree");
            xmlw.openSection("declarations");
            Entrada<String, String>[] decls = new Entrada[2];
            decls[0] = new Entrada<String, String>("name", "name");
            decls[1] = new Entrada<String, String>("type", "String");
            xmlw.autoCloseSection("attributeDecl", decls);
            xmlw.closeLastOpenedSection();
            decls[1] = new Entrada<String, String>("value", "raiz");
            //Procesamos los datos del arbol
            for (Position<E> p : list) {
                currentLevel = depth(p, t);
                if (currentLevel > lastLevel) { //Nodo hijo
                	if(t.isInternal(p)) {
                		xmlw.openSection("branch");
                		decls[1].setValue(p.element().toString());
                		xmlw.autoCloseSection("attribute", decls);
                	}
                	else {
                		xmlw.openSection("leaf");
                		decls[1].setValue(p.element().toString());
                		xmlw.autoCloseSection("attribute", decls);
                	}
                }
                if (currentLevel == lastLevel) { //Nodo hermano
                	xmlw.closeLastOpenedSection();
                	if(t.isInternal(p)) {
                		xmlw.openSection("branch");
                		decls[1].setValue(p.element().toString());
                		xmlw.autoCloseSection("attribute", decls);
                	}
                	else {
                		xmlw.openSection("leaf");
                		decls[1].setValue(p.element().toString());
                		xmlw.autoCloseSection("attribute", decls);
                	}
                }
                if (currentLevel < lastLevel) { //Nodo ancestro
                	for (int i = currentLevel; i <= lastLevel; i++) {
                		xmlw.closeLastOpenedSection();
                	}
                	if(t.isInternal(p)) {
                		xmlw.openSection("branch");
                		decls[1].setValue(p.element().toString());
                		xmlw.autoCloseSection("attribute", decls);
                	}
                	else {
                		xmlw.openSection("leaf");
                		decls[1].setValue(p.element().toString());
                		xmlw.autoCloseSection("attribute", decls);
                		xmlw.closeLastOpenedSection();
                	}
                }
                lastLevel = currentLevel;
            }
        }
        catch (Exception e) {
            //Nunca se llega a este punto
        }
        xmlw.closeXML();
        return xmlw;
    }

    public static LinkedTree<?> XML2Tree(String x) {
        return null;
    }
    
    private int depth(Position<E> p, Tree<E> t){
    	NodoA<E> v2= (NodoA<E>)p;
		try {
			if(t.isRoot(v2))
					return 0;
				else
					return 1+depth(t.parent(v2), t);
		} catch(Exception e){
			e.printStackTrace();
			return 0;
		}
    }
}